package com.ntschy.web.controller.sys;

import cn.hutool.core.util.ObjectUtil;
import com.ntschy.common.constant.HttpStatus;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import com.ntschy.common.core.entity.dataobject.SysConfig;
import com.ntschy.common.core.entity.enums.SysWhetherEnum;
import com.ntschy.sys.entity.dto.config.SysConfigAddUpdateDTO;
import com.ntschy.sys.entity.dto.config.SysConfigPageDTO;
import com.ntschy.sys.entity.mapstruct.SysConfigMapstruct;
import com.ntschy.sys.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author ezreal_geng
 */
@RestController
@RequestMapping("/sys/config")
@Api(tags = "系统配置接口")
@Validated
public class SysConfigController extends BaseController<SysConfig> {

    @Autowired
    private ISysConfigService configService;

    // * ================================================基础增删改查BEGIN============================================= *
    @ApiOperation(value = "查询系统配置列表", notes = "")
    @ApiImplicitParam(name = "queryDto", value = "系统配置列表查询实体类", required = true, dataType = "SysConfigPageDTO")
    //@PreAuthorize("@ss.hasPermi('sys:config:page')")
    @PostMapping("/pageConfig")
    public AjaxResult<List<SysConfig>> pageConfig(@RequestBody SysConfigPageDTO pageDTO) {
        SysConfig queryConfig = SysConfigMapstruct.SYS_CONFIG_MAP_STRUCT.pageDtoToDo(pageDTO);
        List<SysConfig> list = configService.pageConfig(queryConfig);
        return AjaxResult.success("菜单列表", list);
    }


    @ApiOperation(value = "新增系统配置", notes = "")
    @ApiImplicitParam(name = "addDto", value = "新增系统配置实体类", required = true, dataType = "SysConfigAddUpdateDTO")
    //@PreAuthorize("@ss.hasPermi('sys:config:add')")
    @PostMapping("/String")
    public AjaxResult<String> addMenu(@RequestBody SysConfigAddUpdateDTO addDto) {
        if(ObjectUtil.isNotNull(configService.checkConfigNameExist(addDto.getConfigName()))) {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, "系统配置名称已存在");
        } else if (ObjectUtil.isNotNull(configService.checkConfigKeyExist(addDto.getConfigKey()))) {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, "系统配置键已存在");
        }
        SysConfig addConfig = SysConfigMapstruct.SYS_CONFIG_MAP_STRUCT.addUpdateDtoToDo(addDto);
        return toAjax(configService.addConfig(addConfig), "新增系统配置成功", "新增系统配置失败");
    }

    @ApiOperation(value = "更新系统配置", notes = "")
    @ApiImplicitParam(name = "updateDto", value = "更新系统配置实体类", required = true, dataType = "SysConfigAddUpdateDTO")
    //@PreAuthorize("@ss.hasPermi('sys:config:update')")
    @PostMapping("/updateConfig")
    public AjaxResult<String> updateConfig(@RequestBody SysConfigAddUpdateDTO updateDto) {
        SysConfig checkConfigNameExist = configService.checkConfigNameExist(updateDto.getConfigName());
        SysConfig checkConfigKeyExist = configService.checkConfigKeyExist(updateDto.getConfigKey());
        if(ObjectUtil.isNotNull(checkConfigNameExist) && !checkConfigNameExist.getId().equals(updateDto.getId())) {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, "系统配置名称已存在");
        } else if (ObjectUtil.isNotNull(configService.checkConfigKeyExist(updateDto.getConfigKey())) && !checkConfigNameExist.getId().equals(updateDto.getId())) {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, "系统配置键已存在");
        }
        SysConfig updateConfig = SysConfigMapstruct.SYS_CONFIG_MAP_STRUCT.addUpdateDtoToDo(updateDto);
        return toAjax(configService.updateConfig(updateConfig), "更新系统配置成功", "更新系统配置失败");
    }

    @ApiOperation(value = "根据配置键获取配置详情", notes = "")
    @ApiImplicitParam(name = "key", value = "配置键", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('sys:config:detail')")
    @GetMapping("/fetchConfigByKey")
    public AjaxResult<SysConfig> fetchConfigByKey(@NotBlank(message = "配置键不能为空")  String key) {
        SysConfig config = configService.fetchConfigByKey(key);
        return AjaxResult.success("获取系统配置成功", config);
    }

    @ApiOperation(value = "根据菜单ID删除菜单", notes = "")
    @ApiImplicitParam(name = "menuId", value = "菜单ID", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('sys:config:delete')")
    @GetMapping("/deleteConfig")
    public AjaxResult<String> deleteConfig(@NotBlank(message = "配置ID不能为空") @Length(min = 36, max = 36, message = "配置ID不合法") String id) {

        SysConfig config = configService.fetchConfig(id);
        if(config.getConfigType() == SysWhetherEnum.IS) {
            return AjaxResult.error(HttpStatus.ERROR, "系统内置配置,不允许删除");
        }
        return toAjax(configService.deleteConfig(id), "删除系统配置成功", "删除系统配置失败");
    }
    // * =================================================基础增删改查END============================================== *

}
